package com.fan.shop.mock;

import java.util.HashMap;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.fan.shop.dao.CategoryDaoImpl;
import com.fan.shop.domain.Category;
import com.fan.shop.hibernate.utils.HibernateUtil;
import com.fan.shop.services.CategoryServiceImpl;

public class CategoryMock {

	public void insertCategories() {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();

		Category clotheCat = new Category();

		clotheCat.setParent(null);
		clotheCat.setName("Clothe");
		clotheCat.setDescription("All kinds of Clothe");
		session.save(clotheCat);

		Category swimsuitCat = new Category();
		swimsuitCat.setParent(clotheCat.getId());
		swimsuitCat.setName("Swimsuit");
		swimsuitCat.setDescription("All kinds of Swimsuit");

		session.save(swimsuitCat);

		Category maleSwimsuitCat = new Category();
		maleSwimsuitCat.setParent(swimsuitCat.getId());
		maleSwimsuitCat.setName("Male Swimsuit");
		maleSwimsuitCat.setDescription("Male swimsuit-swim trunk");
		session.save(maleSwimsuitCat);

		Category maleSwimTrunkCat = new Category();
		maleSwimTrunkCat.setParent(maleSwimsuitCat.getId());
		maleSwimTrunkCat.setName("Male SwimTrunk");
		maleSwimTrunkCat.setDescription("Male Swim trunk");
		session.save(maleSwimTrunkCat);

		Category femaleSwimsuitCat = new Category();
		femaleSwimsuitCat.setParent(swimsuitCat.getId());
		femaleSwimsuitCat.setName("Female Swimsuit");
		femaleSwimsuitCat.setDescription("Female Swimsuit");
		session.save(femaleSwimsuitCat);

		Category bikiniCat = new Category();
		bikiniCat.setParent(femaleSwimsuitCat.getId());
		bikiniCat.setName("Bikini");
		bikiniCat.setDescription("Bikini ");
		session.save(bikiniCat);

		Category twoPiecesCat = new Category();
		twoPiecesCat.setParent(femaleSwimsuitCat.getId());
		twoPiecesCat.setName("Two Pieces");
		twoPiecesCat.setDescription("Two pieces female swimsuits");
		session.save(twoPiecesCat);

		Category onePiecesCat = new Category();
		onePiecesCat.setParent(femaleSwimsuitCat.getId());
		onePiecesCat.setName("One Pieces");
		onePiecesCat.setDescription("One pieces Swimsuit");
		session.save(onePiecesCat);
		
		
		Category kidsSwimsuitCat = new Category();
		kidsSwimsuitCat.setParent(swimsuitCat.getId());
		kidsSwimsuitCat.setName("Kids Swimsuit");
		kidsSwimsuitCat.setDescription("Kids Swimsuit");
		session.save(kidsSwimsuitCat);

		transaction.commit();

		session.close();

	}

	public String buildTree(CategoryServiceImpl service, Category category,
			HashMap<String, String> map) {
		if (service.isLeaf(category)) {
			return "<li><span class=\"file\">" + category.getName()
					+ "</span></li>";
		}
		String result = "";
		
		result = "<li><span class=\"folder\">" + category.getName() + "</span>";
		for (Category cat : service.getSubCategories(category)) {
			
			result+= "<ul>" + buildTree(service, cat, map) + "</ul>";
			
		}

		 return result + "</li>";
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		Transaction transaction = session.beginTransaction();

		CategoryDaoImpl categoryDao = new CategoryDaoImpl();
		categoryDao.setSessionFactory(sessionFactory);

		categoryDao.getSessionFactory().getCurrentSession();
		CategoryServiceImpl service = new CategoryServiceImpl();

		service.setCategoryDao(categoryDao);

//		CategoryMock test = new CategoryMock();
//
		System.out.println(service.getCategory("Clothe"));
//		String result = test.buildTree(service, service.getCategory("Clothe"),
//						new HashMap<String, String>()) ;
//		System.out.println(result);
		
		//CategoryMock mock = new CategoryMock();
		//mock.insertCategories();
		
		System.out.println(service.getColumnNames().size());
		for(String name : service.getColumnNames())
		{
			System.out.println(name);
		}
		
		
	}

}
